//
//  ScanningCodeViewController.m
//  XiXi
//
//  Created by HawkingWang on 16/9/30.
//  Copyright © 2016年 xi-xi.cc. All rights reserved.
//

#import "ScanningCodeViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ScanningCodeViewController () <AVCaptureMetadataOutputObjectsDelegate>

/**扫描用到的类*/
@property (strong,nonatomic) AVCaptureDevice * device;
@property (strong,nonatomic) AVCaptureDeviceInput * input;
@property (strong,nonatomic) AVCaptureMetadataOutput * output;
@property (strong,nonatomic) AVCaptureSession * session;
@property (strong,nonatomic) AVCaptureVideoPreviewLayer * preview;
@property (weak, nonatomic) IBOutlet UIView *scanView;

/**剧目图片*/
@property (weak, nonatomic) IBOutlet UIImageView *image;
/**剧目名*/
@property (weak, nonatomic) IBOutlet UILabel *name;
/**演出时间*/
@property (weak, nonatomic) IBOutlet UILabel *time;
/**剧院*/
@property (weak, nonatomic) IBOutlet UILabel *theatre;
/**演出厅*/
@property (weak, nonatomic) IBOutlet UILabel *hall;

/**座位号*/
@property (weak, nonatomic) IBOutlet UILabel *seat;
/**手机号*/
@property (weak, nonatomic) IBOutlet UILabel *phone;
/**等级*/
@property (weak, nonatomic) IBOutlet UILabel *grade;

@end

@implementation ScanningCodeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置导航栏
    [self setNav];
    
    //创建扫描用到的类
    [self createScan];
    
    [self setInformation];
}

- (void)setNav{
    UIBarButtonItem *close = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"off"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = close;
}
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createScan{
    // Device
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Input
    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
    // Output
    _output = [[AVCaptureMetadataOutput alloc] init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // Session
    _session = [[AVCaptureSession alloc] init];
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    //连接输入和输出
    if ([_session canAddInput:self.input])
    {
        [_session addInput:self.input];
    }
    
    if ([_session canAddOutput:self.output])
    {
        [_session addOutput:self.output];
    }
    //设置条码类型
    _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeQRCode];
    //添加扫描画面
    _preview =[AVCaptureVideoPreviewLayer layerWithSession:_session];
    _preview.videoGravity =AVLayerVideoGravityResizeAspectFill;
    _preview.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH);
    [_scanView.layer insertSublayer:_preview atIndex:0];
    //开始扫描
    [_session startRunning];
}

#pragma mark === AVCaptureMetadataOutputObjectsDelegate ===
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    NSString *stringValue;
    if ([metadataObjects count] >0){
        //停止扫描
        [_session stopRunning];
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
        [EBUtility prompt:@"扫描成功" with:self];
    }
}

- (void)setInformation{
    _image.backgroundColor = [UIColor colorWithRed:VALUE green:VALUE blue:VALUE alpha:1];
    _name.text = @"阿加莎推理巨作——爱乐汇·拉得斯基进行曲——世界";
    _time.text = @"2016.01.30 19:30 ~ 21:30";
    _theatre.text = @"西溪天堂艺术中心剧院名称";
    _hall.text = @"Interting演出厅名称";
    
    _seat.text = @"第1排13座";
    _phone.text = @"156***6567";
    _grade.text = @"钻石卡";
}

@end
